Skip to content

Conversation

@irfanajaffer
Copy link
Collaborator

Task-972104-How to silent print PDF document using Syncfusion.Pdf.Net.Core Package?

Task details: https://dev.azure.com/EssentialStudio/Document%20Processing%20Libraries/_workitems/edit/972104

# Visual Studio Version 17
VisualStudioVersion = 17.13.35617.110 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "How_to_silent_print_a_PDF_document", "How_to_silent_print_a_PDF_document\How_to_silent_print_a_PDF_document.csproj", "{C39DF450-AD95-418F-8946-A0F0FEB51117}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project file and folder name are mentioned wrongly in the sln file. How it will be working on your end?

// Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
// Load the PDF document as a stream
using (FileStream inputStream = new FileStream("Data/Input.pdf", FileMode.Open, FileAccess.ReadWrite))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify the code like below.

using Syncfusion.PdfToImageConverter;
using System.Drawing;
using System.Drawing.Printing;

// Initialize the PdfToImageConverter with the path to the PDF file
using (PdfToImageConverter imageConverter = new PdfToImageConverter())
{
    // Load the PDF document
    imageConverter.Load(new FileStream("Data/Input.pdf", FileMode.Open, FileAccess.Read));

    //Convert all the PDF pages to images
    Stream[] images = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);

    //Create a print document object to print the images
    using (PrintDocument printDocument = new PrintDocument())
    {
        int currentPageIndex = 0;
        //Handle the PrintPage event to print each image
        printDocument.PrintPage += (sender, e) =>
        {
            Stream imageStream = images[currentPageIndex];
            imageStream.Position = 0; // Reset stream position
            Image currentImage = Image.FromStream(imageStream);
            //Draw the current image on the print page
            e.Graphics.DrawImage(currentImage, e.PageBounds);
            //Move to the next page
            currentPageIndex++;
            //Check if there are more pages to print
            e.HasMorePages = currentPageIndex < images.Length;
        };
        //Print the document
        printDocument.Print();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants